import { remove, getList, save } from '@/api/park/contract'
import { getSingleParking, getParkingSelects } from '@/api/park/info'
import { getParkingQuotes } from '@/api/park/quote'
import { getSingleCust } from '@/api/cust/cust'
import { getSelects } from '@/api/house/community'
import { getCustSelects } from '@/api/cust/custhouseref'
import {isIntegerBig} from "../../../utils/validate"
import { getOptions } from '@/api/option'

export default {
  data() {
    return {
      formVisible: false,
      formTitle: '添加车位合同',
      isAdd: true,
      form: {
        custId:'',
        communityId:'',
        parkingId:'',
        quoteId:'',
        quoteType:'',
        quoteRange:'',
        quotePrice:'',
        quoteUnit:'',
        realPrice:'',
        signDate:'',
        startDate:'',
        endDate:'',
        contractFileId:'',
        id: ''
      },
      listQuery: {
        page: 1,
        limit: 20,
        id: undefined,
        communityId: undefined,
        quoteType: undefined,
        yesOrNo: undefined
      },
      total: 0,
      list: null,
      listLoading: true,
      selRow: {},
      viewParkingVisible: false,
      viewParkingTitle: '查看车位信息',
      viewParking:{},
      viewCustVisible: false,
      viewCustTitle: '查看客户信息',
      viewCust:{},
      options:{
        communitys:[],
        custIds:[],
        custIdsCopy:[],
        parkings:[],
        parkingsCopy:[],
        quotes:[],
        yesOrNo:[],
      },
      value:'',
      parkValue:'',
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  computed: {

    //表单验证
    rules() {
      return {
        communityId: [
          { required: true, message: '请选择社区', trigger: 'blur' }
        ],
        custId: [
          { required: true, message: '请选择客户', trigger: 'blur' }
        ],
        parkingId: [
          { required: true, message: '请选择车位', trigger: 'blur' }
        ],
        quoteId: [
          { required: true, message: '请选择报价方式', trigger: 'blur' }
        ],
        realPrice:[
          { required: true, message: '请输入合同金额', trigger: 'blur' },
          {
            validator (rule, value, callback) {
              if (isIntegerBig(value)) {
                callback()
              } else {
                callback(new Error('请输入正整数'))
              }
            },
            trigger: 'blur'
          }
        ],
        signDate:[
          { required: true, message: '请选择签订日期', trigger: 'blur' }
        ],
        startDate:[
          { required: true, message: '请选择开始日期', trigger: 'blur' }
        ],
        contractFileId: [
          { required: true, message: '请选择合同文件', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.init()
    this.initCommunitys()
    this.initOptions()
  },
  methods: {
    init() {
      this.fetchData()
    },
    initCommunitys(){
      getSelects().then(response => {
        this.options.communitys = response.data;
      })
    },
    initOptions(){
      getOptions('YesOrNo_QuoteType').then(response => {
        this.options.yesOrNo = response.data.maps.YesOrNo;
        this.options.quotes = response.data.maps.QuoteType;
      })
    },
    getCustsAndParking(){
      this.form.custId = '';
      this.form.parkingId = '';
      this.getCusts();
      this.getParkings();
    },
    getCusts(){
      getCustSelects(this.form.communityId).then(response => {
        this.options.custIds = response.data;
        this.options.custIdsCopy = response.data;
      })
    },
    getParkings(){
      this.options.quotes = [];
      this.form.quoteType = '';
      this.form.quoteRange = '';
      this.form.quotePrice = '';
      this.form.quoteUnit = '';
      getParkingSelects(this.form.communityId).then(response => {
        this.options.parkings = response.data;
        this.options.parkingsCopy = response.data;
      })
    },
    custFilter(val) {
      this.value = val;
      if(val){
        this.options.custIds = this.options.custIdsCopy.filter((item) => {
          if (!!~item.custName.indexOf(val) || !!~item.custName.toUpperCase().indexOf(val.toUpperCase())) {
            return true
          }
        })
      } else {
        this.options.custIds = this.options.custIdsCopy;
      }
    },
    parkingFilter(val) {
      this.parkValue = val;
      if(val){
        this.options.parkings = this.options.parkingsCopy.filter((item) => {
          if (!!~item.title.indexOf(val) || !!~item.title.toUpperCase().indexOf(val.toUpperCase())) {
            return true
          }
        })
      } else {
        this.options.parkings = this.options.parkingsCopy;
      }
    },
    getQuotes(){
      this.options.quotes = []
      getParkingQuotes(this.form.parkingId).then(response => {
        this.options.quotes = response.data;
      })
    },
    selectQuote(){
      this.options.quotes.forEach((item, index) => {
        if(item.id === this.form.quoteId){
          this.form.quoteType = item.quoteType
          this.form.quoteRange = item.quoteRange
          this.form.quoteUnit = item.quoteUnit
          this.form.quotePrice = item.quotePrice

        }
      })
    },

    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.records
        this.listLoading = false
        this.total = response.data.total
      })
    },
    queryParking(parkingId){
      this.viewParkingVisible = true
      getSingleParking(parkingId).then(response => {
        this.viewParking = response.data;
      })
    },
    queryCust(custId){
      this.viewCustVisible = true
      getSingleCust(custId).then(response => {
        this.viewCust = response.data;
      })
    },
    search() {
      this.fetchData()
    },
    reset() {
      this.listQuery.id = ''
      this.listQuery.communityId = ''
      this.listQuery.quoteType = ''
      this.listQuery.yesOrNo = ''
      this.fetchData()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleClose() {

    },
    fetchNext() {
      this.listQuery.page = this.listQuery.page + 1
      this.fetchData()
    },
    fetchPrev() {
      this.listQuery.page = this.listQuery.page - 1
      this.fetchData()
    },
    fetchPage(page) {
      this.listQuery.page = page
      this.fetchData()
    },
    changeSize(limit) {
      this.listQuery.limit = limit
      this.fetchData()
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow
    },
    resetForm() {
      this.form = {
        custId:'',
        communityId:'',
        parkingId:'',
        quoteId:'',
        quoteType:'',
        quotePrice:'',
        quoteRange:'',
        quoteUnit:'',
        realPrice:'',
        signDate:'',
        startDate:'',
        endDate:'',
        contractFileId:'',
        id: ''
      }
    },
    add() {
      this.resetForm()
      this.formTitle = '添加车位合同',
      this.formVisible = true
      this.isAdd = true
    },
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          save({
            custId:this.form.custId,
            communityId:this.form.communityId,
            parkingId:this.form.parkingId,
            quoteId:this.form.quoteId,
            quoteType:this.form.quoteType,
            quotePrice: this.form.quotePrice,
            quoteRange:this.form.quoteRange,
            quoteUnit:this.form.quoteUnit,
            realPrice:this.form.realPrice,
            signDate:this.form.signDate,
            startDate:this.form.startDate,
            endDate:this.form.endDate,
            contractFileId:this.form.contractFileId,
            id: this.form.id
          }).then(response => {
            this.$message({
              message: this.$t('common.optionSuccess'),
              type: 'success'
            })
            this.fetchData()
            this.formVisible = false
          })
        } else {
          return false
        }
      })
    },
    checkSel() {
      if (this.selRow && this.selRow.id) {
        return true
      }
      this.$message({
        message: this.$t('common.mustSelectOne'),
        type: 'warning'
      })
      return false
    },
    // edit() {
    //   if (this.checkSel()) {
    //     this.isAdd = false
    //     this.form = this.selRow
    //     this.formTitle = '编辑车位合同'
    //     this.formVisible = true
    //   }
    // },
    remove() {
      if (this.checkSel()) {
        var id = this.selRow.id
        this.$confirm(this.$t('common.deleteConfirm'), this.$t('common.tooltip'), {
          confirmButtonText: this.$t('button.submit'),
          cancelButtonText: this.$t('button.cancel'),
          type: 'warning'
        }).then(() => {
          remove(id).then(response => {
            this.$message({
              message: this.$t('common.optionSuccess'),
              type: 'success'
            })
            this.fetchData()
          }).catch( err=> {
            this.$notify.error({
              title: '错误',
              message: err
            })
          })
        }).catch(() => {
        })
      }
    }

  }
}
